<!DOCTYPE html>
<html class="bg-white" xmlns:th="http://www.thymeleaf.org">
<!--头文件引入-->
<head th:include="include :: header"></head>
<link rel="stylesheet" th:href="@{/css/admin.css}" media="all" />
<link rel="stylesheet" th:href="@{/plugins/zTree/css/zTreeStyle/zTreeStyle.css}" media="all" />
<body>
<form id="roleForm" lay-filter="roleForm" class="layui-form model-form">
    <input name="roleId" type="hidden"/> <!--角色ID-->
    <div class="layui-form-item">
        <label class="layui-form-label">角色名称</label>
        <div class="layui-input-block">
            <input name="roleName" placeholder="请输入角色名称" type="text" class="layui-input" maxlength="20"
                   lay-verify="required" required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">权限字符</label>
        <div class="layui-input-block">
            <input name="roleKey" placeholder="请输入权限字符" type="text" class="layui-input" maxlength="20"
                   lay-verify="required" required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">显示顺序</label>
        <div class="layui-input-block">
            <input name="roleSort" placeholder="请输入显示顺序" type="text" class="layui-input" lay-verify="" required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="radio" name="status" value="0" title="启用" checked/>
            <input type="radio" name="status" value="1" title="停用"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea name="remark" placeholder="请输入内容" class="layui-textarea" maxlength="200"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">菜单权限</label>
        <div class="layui-input-block">
            <ul id="treeAuth" class="ztree" style="padding: 10px 0px 20px 10px;"></ul>
        </div>
    </div>


    <div class="layui-form-item text-right">
        <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        <button class="layui-btn" lay-filter="btnSubmit" lay-submit>保存</button>
    </div>
</form>

<!-- js部分 -->
<div th:include="include::footer"></div>
<script type="text/javascript" th:src="@{/js/common.js}"></script>
<script type="text/javascript" th:src="@{/plugins/zTree/js/jquery.ztree.all-3.5.min.js}"></script>
<script>
    layui.use(['layer', 'form','admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var admin = layui.admin;
        var url = ctx+"system/role/add";
        var treeUrl = ctx+"system/menu/roleMenuTreeData";
        var setting = {
            check: {enable: true},
            data: {
                simpleData: {enable: true}
            }
        };
        // 回显数据
        var role = admin.getTempData('t_role');
        if(role){
            var roleId = role.roleId;
            console.log("roleId:"+roleId);
            form.val('roleForm', role);
            url = ctx+"system/role/edit";
            treeUrl = ctx+"system/menu/roleMenuTreeData?roleId="+roleId;
        }
            console.log("treeUrl:"+treeUrl);
        //监听指定开关
   /*     form.on('switch(switchTest)', function (data) {
            if (this.checked) {
                $("#status").val(0); //启用
            }
            else {
                $("#status").val(1);//停用
            }
        });*/

   //加载权限树
    $.get(treeUrl, {}, function (data) {
        $.fn.zTree.init($('#treeAuth'), setting, data);
    }, 'json');

        var menuIdsJson='';
        // 表单提交事件
        form.on('submit(btnSubmit)', function (data) {
            layer.load(2);
            console.log(url);
            var treeObj = $.fn.zTree.getZTreeObj('treeAuth');
            var nodes = treeObj.getCheckedNodes(true);
            var ids = new Array();
            for (var i = 0; i < nodes.length; i++) {
                ids[i] = nodes[i].id;
            }
            console.log("menuids:"+ids);

            menuIdsJson = JSON.stringify(ids);
            console.log("menuIdsJson"+menuIdsJson);
            addRole(data);
            return false;
        });

        function addRole(data) {
            $.post(url
                ,{
                roleId:data.field.roleId,
                roleName: data.field.roleName,
                roleKey: data.field.roleKey,
                roleSort: data.field.roleSort,
                status: data.field.status,
                remark: data.field.remark,
                menuIdsJson: menuIdsJson
            }, function (data) {
                layer.closeAll('loading');
                if (data.code == 0) {
                    top.layer.msg(data.msg, {icon: 1});
                    admin.putTempData('formOk', true);  // 操作成功刷新表格
                    // 关闭当前iframe弹出层
                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                } else {
                    top.layer.msg(data.msg, {icon: 2});
                }
            });
        }


    });
</script>
</body>
</html>